/**
 * Created by JasonChou on 17/7/10.
 */
const {getTattooer, getProducts} = require('../../apis');
const wx = require('../../libs/wx.js');
const Page = require('../../libs/BasePage');
const app = getApp();
const config = require('../../config');

Page({
    data: {
        clerk: {},
        products: [],
        company: {},
        query: {}
    },
    onLoad (query) {
        this.setData({query});

        getTattooer(query.userId).then(res => {
            this.setData({clerk: res.data.data})
        });

        getProducts(query.userId).then(res => {
            this.setData({products: res.data.data})
        });

        app.getCompanyDetail().then((res) => {

            const {clerks, company, sales} = res;

            this.setData({company});
        })
    },

    onShareAppMessage() {
        return {
            title: this.data.clerk.nickname + ' - 纹身作品集',
            path: '/pages/clerk/clerk?userId=' + this.data.query.userId,
        }
    },

    previewImage(e){
        wx.previewImage({
            current: e.currentTarget.dataset.url,
            urls: e.currentTarget.dataset.urls
        })
    },

    onCallClick(){
        wx.makePhoneCall({
            phoneNumber: this.data.company.phoneNum || ''
        })
    },

    goToMap() {
        wx.openLocation({
            longitude: this.data.company.location[0],
            latitude: this.data.company.location[1],
            name: config.name,
            address: this.data.company.address
        });
    },

    loadMore(){
        const {products, query} = this.data;
        getProducts(query.userId, products[products.length - 1].created).then(res => {
            this.setData({products: products.concat(res.data.data)})
        });

    }
});